<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item,index) in commentList" :key="index" :title="item.content">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.auth_photo"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | relativeTime}}</span>
            <van-button size="mini" type="default" @click="$emit('replay-show',true)">回复{{item.reply_count}}</van-button>
          </p>
        </div>
        <van-icon @click="onCommentLike(item)" slot="right-icon" :class="{liked:item.is_liking}" :name="item.is_liking ? 'good-job' : 'good-job-o'" >
          <span style="font-size:12px;position:relative;top:-2px;">{{item.like_count===0?'赞':item.like_count}}</span>
        </van-icon>

      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容">
        <van-button  slot="button" size="mini" type="info">发布111</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->

  </div>
</template>

<script>
import { addCommentLike, deleteCommentLike, getComments } from '@/api/comment'

export default {
  name: 'ArticleComment',
  props: {
    source: {
      type: [Number, String, Object],
      required: true
    },
    commentList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null, // 请求下一页数据的页码 数据是连续
      totalCount: null
    }
  },
  created () {
    this.onLoad()
  },
  methods: {
    async onCommentLike (comment) {
      // 如果已经赞了则取消点赞
      try {
        if (comment.is_liking) {
          await deleteCommentLike(comment.com_id)
        } else {
        // 如果没有赞，则点赞
          await addCommentLike(comment.com_id)
        }

        this.$toast('操作成功')
      } catch (err) {
        console.log(err)
      }
      // 更新视图状态
      comment.is_liking = !comment.is_liking
    },
    async onLoad () {
      console.log(this.source)
      // 1. 请求获取数据
      const { data } = await getComments({
        type: 'a', // 评论类型，a-对文章(article)的评论，c-对评论(comment)的回复
        source: this.source.toString(), // 源id，文章id或评论id
        offset: this.offset, // 获取评论数据的偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数据
        limit: 10 // 每页大小
      })

      console.log(data)
      // 2. 将数据添加到列表中
      const { results, total_count: totalCount } = data.data
      this.commentList.push(...results)
      this.totalCount = totalCount
      console.log(this.totalCount)

      this.$emit('onload-success', data.data)

      this.loading = false// 关闭loading效果

      // 4. 判断是否还有数据
      if (results.length) {
        this.offset = data.data.last_id // 更新获取下一页数据的页码
      } else {
        this.finished = true // 没有数据了，关闭加载更多
      }
    }
  }
}
</script>

<style scoped lang="less">
  .publish-wrap {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }

  .van-list {
    margin-bottom: 45px;
  }
  .liked{
    color:red;
  }
</style>
